//
//  PKRadioMainViewController.m
//  PianKe
//
//  Created by lanou on 15/9/25.
//  Copyright (c) 2015年 李伟伟. All rights reserved.
//

#import "PKRadioMainViewController.h"
#import "PKRadioMainListTableViewCell.h"
#import "PKRequestManager.h"
#import "PKRadioTableViewHeaderView.h"
#import "PKRadioDetailCell.h"
#import "PKRadioDetailViewController.h"
#import "LORefresh.h"
@interface PKRadioMainViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property(nonatomic, strong)UITableView *mainTableView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)NSMutableArray *imageArray;
@property(nonatomic, strong)PKRadioTableViewHeaderView *headerView;
@end

@implementation PKRadioMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    self.titleLabel.text = @"电台";
    _headerView = [[PKRadioTableViewHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WITDTH, 290)];
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WITDTH, SCREEN_HEIGHT - 70) style:UITableViewStylePlain];
    _mainTableView.tableHeaderView = _headerView;
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
     _mainTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_mainTableView];
   
    [_mainTableView registerClass:[PKRadioMainListTableViewCell class] forCellReuseIdentifier:@"PKRadioMainListTableViewCell"];
    
    [self requestData];
    
    
    
    __weak PKRadioMainViewController *newSelf = self;
    
//    [_mainTableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
//        [newSelf requestData];
//    }];
    
    [_mainTableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
        [newSelf requestData];
    }];
    LORefresh *view = _mainTableView.gifHeader;
    [view setGifName:@"demo"];
    view.refreshLayoutType = LORefreshLayoutTypeTopIndicator; //图片和文字的位置设置  ，此选择为上下布局



}


-(void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"auth"] = @":";
    parDic[@"version"] = @"3.0.2";
       
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/ting/radio" parDic:parDic finish:^(NSData *data)
    {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        
        
        
        
        NSArray *listArray = dic[@"data"][@"alllist"];
        _dataSourceArray = [NSMutableArray array];
        for (NSDictionary *modelDic in listArray)
        {
            PKRadioMainListModel *model = [[PKRadioMainListModel alloc]init];
            [model setValuesForKeysWithDictionary:modelDic];
            [_dataSourceArray addObject:model];
            
        }
        
        _imageArray = [NSMutableArray array];
        
        NSArray *carouselArray = dic[@"data"][@"carousel"];
        for (NSDictionary *scrollDic in carouselArray) {
            [_imageArray addObject:scrollDic[@"img"]];
        }
        
        NSArray *hotlistArray = dic[@"data"][@"hotlist"];
        for (NSDictionary *buttonDic in hotlistArray) {
            [_imageArray addObject:buttonDic[@"coverimg"]];
        }
        
        [_headerView configureData:_imageArray];
        
        [_mainTableView reloadData];
        NSLog(@"------------------------------------");
//        [_mainTableView.defaultHeader endRefreshing];// 可选择用什么结束刷新
        [_mainTableView.gifHeader endRefreshing];
        
        
        
    }
        error:^(NSError *error) {
                                    NSLog(@"电台请求失败 ----%@",error) ;
                                }
     ];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKRadioMainListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PKRadioMainListTableViewCell" forIndexPath:indexPath];
    cell.configureModel = _dataSourceArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [PKRadioMainListTableViewCell heightForCell ];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKRadioDetailViewController *detailVC = [[PKRadioDetailViewController alloc] init];
    detailVC.radioid = [_dataSourceArray[indexPath.row] radioid];
    detailVC.titleLabel.text = [_dataSourceArray[indexPath.row] title];
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
